<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\Group;

class GroupController extends Controller
{
    use ModelForm;
    protected $headerStr = '采集群';

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->description('编辑' . $this->headerStr);
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->headerStr . '管理');
            $content->description($this->headerStr . '列表...');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->headerStr . '管理');
            $content->description('修改' . $this->headerStr . '...');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('管理');
            $content->description($this->headerStr . '...');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Group::class, function (Grid $grid) {
            $grid->id('编号');
            // $grid->avatar('头像')->display(function($avatar){
            //     return '<img src="'.$avatar.'">';
            // });
            $grid->group_num('qq群号');
            $grid->group_name('网站名称');
            $grid->user_nicename('采集群名称');
            // $grid->online_count('放单数');
            $grid->online_count('在线商品');
            // $grid->group_sort('排名')->sortable();
            $grid->entry_day('入驻时长');
            $grid->actions(function ($actions) {
                //$actions->disableDelete();
                //$actions->disableView();
                if(method_exists($actions,'disableView')){
                    $actions->disableView();
                }
            });
            $grid->paginate(env('Admin_Limit'));
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Group::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('user_nicename', '群美名');
            $form->text('group_num', 'qq群号');
            $form->text('group_order', 'qq序号')->placeholder('从1开始，依次递增，越小优先级越高');
            $form->text('create_time', 'qq群创建时间');
            $form->text('group_name', 'qq群名');
            $form->text('group_nickname', 'qq群第二昵称明');
            $states = [
                'on' => ['value' => 1, 'text' => '禁用', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '正常', 'color' => 'danger'],
            ];
            $form->switch('group_status', '状态')->states($states);
            $states = [
                'on' => ['value' => 1, 'text' => '隐藏', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '显示', 'color' => 'danger'],
            ];
            $form->switch('if_show', '是否显示')->states($states);
            $form->number('sort', '排序大小')->placeholder('从1开始，依次递增，越小优先级越高');
            $form->text('avatar', '采集群头像');
            $form->number('online_count', '在线商品数量');
            $form->number('group_sort', '平台排名');
            $form->number('jifen', '平台积分');
            $form->textarea('signature', '平台积分');
            $form->select('user_status', '审核状态')->options(['0' => '禁用', '1', '正常', '2' => '未验证', '3' => '已删除']);
            $states = [
                'on' => ['value' => 1, 'text' => '已认证', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '未认证', 'color' => 'danger'],
            ];
            $form->switch('centifi', '是否官方认证')->states($states);
            $states = [
                'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '不是', 'color' => 'danger'],
            ];
            $form->switch('cow', '是否官方认证')->states($states);
            $form->text('city', '来自哪个城市');
            $form->textarea('centifi_info', '认证信息');
            $form->text('qq', 'QQ号码');
            $form->number('entry_day', '入驻天数');
            $form->text('group_type', '采集群类型');
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                // $tools->disableListButton(); // 去掉跳转列表按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                //$tools->disableView(); // 去掉跳转列表按钮
            });
        });
    }
}